package com.irdstudio.efp.esb.api.rest.nls;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SEQ;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.efp.esb.api.bean.nls.ReqLoanAppBean;
import com.irdstudio.efp.esb.common.constant.ESBServerConstance;
import com.irdstudio.efp.esb.common.constant.nls.NlsProdCdEnum;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;

/**
 * 服务名称 ：30220001(贷款申请管理)<br>
 * 服务场景名称：01(进件申请) <br>
 * 服务描述：用户向相关银行申请贷款业务的处理 <br>
 * 服务场景描述：用户通过相关平台向网贷系统发送证件类型、贷款金额、天数等相关资料信息，由此发起贷款申请； <br>
 * 
 * @author huangwb
 * @since 2019年1月18日 下午7:40:24
 * @version 1.0
 */
@Controller
@RequestMapping("/esb")
public class NlsController {
	private static Logger logger = LoggerFactory.getLogger(NlsController.class);
	public static final PatternDescriptor PATTERN = DefaultPatternDescriptor.of("S" + SEQ, 9);
	
	/**
	 * 根据渠道，产品代码分发场景class，主要用于分辨获取字段注解的校验
	 * TODO <br>
	 * 0.2:heyingfeng:2019年3月14日 下午8:20:07:TODO <br>
	 * @author heyingfeng
	 * @since 2019年3月14日 下午8:20:07
	 * @date 2019年3月14日 下午8:20:07
	 * @version 0.1 初始为0.1,后续变更则修改
	 */
	@PostMapping("/SERVICE_30220001_01")
	public String doRecv(HttpServletRequest request) throws Exception {
		logger.info("校验 ESB请求体对象！");
		EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
		String esbReqBody = (String) request.getAttribute("esbReqBody");
		ReqLoanAppBean reqLoanAppBean = JSONObject.parseObject(esbReqBody, ReqLoanAppBean.class);
		String svcCd = esbReqServiceBean.getSysHead().getSvcCd(); // 服务代码
		String scnCd = esbReqServiceBean.getSysHead().getScnCd(); // 场景
		String prdCode = reqLoanAppBean.getProdCd();
		String chanFlg = reqLoanAppBean.getChanFlg();
		String tradeCode = "";
		if(NlsProdCdEnum.MYD.getValue().equals(prdCode) || NlsProdCdEnum.ZXD.getValue().equals(prdCode)) {//百度
			tradeCode = ESBServerConstance.SERVICE_KEY_HEAD + svcCd + "_" + scnCd + "_" + chanFlg;// 交易码
		}
		if(NlsProdCdEnum.MS.getValue().equals(prdCode)) {//马上
			tradeCode = ESBServerConstance.SERVICE_KEY_HEAD + svcCd + "_" + scnCd + "_" + prdCode;// 交易码
		}
		return "forward:/esb/" + tradeCode;
	}

}
